﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

using MT.eVotePRS.Device;
using MT.eVotePRS.Model;

using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Logic;

namespace MT.eVotePRS.Presentation.Presenter
{
    public class LostPresenter
    {
        private IViewLost view;

        private StudentActions studentAction;
        private HandsetActions handsetAction;
        private InventoryTxnActions txnAction;

        public StudentActions StudentAction
        {
            get { return studentAction; }
            set { studentAction = value; }
        }
        public HandsetActions HandsetAction
        {
            get { return handsetAction; }
            set { handsetAction = value; }
        }
        public InventoryTxnActions TxnAction
        {
            get { return txnAction; }
            set { txnAction = value; }
        }


        public void SetView(IViewLost view)
        {
            this.view = view;
            view.PropertyChanged += OnViewPropertyChanged;
            view.BarcodeEntered += OnBarcodeEntered;
            view.StudentNoEntered += OnStudentNoEntered;
            view.ConfirmClicked += OnConfirmClicked;
        }

        void OnBarcodeEntered(object sender, EventArgs e)
        {
            StudentInfo student = studentAction.FindStudentByBarcode(view.Student.Barcode);
            checkStudentStatus(student);
        }

        void OnStudentNoEntered(object sender, EventArgs e)
        {
            StudentInfo student = studentAction.FindStudentByNo(view.Student.StudentNo);
            checkStudentStatus(student);
        }

        void checkStudentStatus(StudentInfo student)
        {
            if (student != null)
            {
                view.Student = student;
                InventoryTxn txn = txnAction.CheckStudentStatus(student.StudentNo);
                if (txn != null)
                {
                    if (txn.Status == Util.Constants.Status_CheckIn.Id)
                    {
                        throw new Exception(string.Format("{0} ({1}) was Checked In.", student.StudentName, txn.StudentID));
                    }
                    view.Handset.MAC = txn.HandsetID;
                }
            }
            else
            {
                throw new Exception("Student not found!");
            }
        }

        private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        private void OnConfirmClicked(object sender, EventArgs e)
        {
            // TODO: Save Student and Check Out transaction
            try
            {

                InventoryTxn inventoryTxn = new InventoryTxn();
                inventoryTxn.HandsetID = view.Handset.MAC;
                inventoryTxn.StudentID = view.Student.StudentNo;
                inventoryTxn.TxnTime = DateTime.Now;
                inventoryTxn.Remark = view.EventRemark;
                inventoryTxn.Status = view.ToStatus.Id;
                inventoryTxn.UserEvent = view.ToStatus.StatusDesc;
                inventoryTxn.NeedPay = false;
                inventoryTxn.PaymentNo = string.Empty;
                inventoryTxn.IsOverride = false;
                inventoryTxn.OverrideStatus = -1;

                object txnID = txnAction.CreateNewTxn(inventoryTxn);
                if (txnID != null && Convert.ToInt32(txnID) > 1)
                {

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
